import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;


public class ClassDao {
	public static ArrayList<Class1> queryclassList() {
		ArrayList<Class1> classList = new ArrayList<>();
		Connection conn = (Connection) ConnectionUtil.getConnection();
		Statement stmt = null;
		try {
			// 执行查询
			System.out.println(" 实例化Statement对象...");
			stmt = (Statement) conn.createStatement();
			String sql;
			sql = "SELECT bjbh, bjmc, ssxy, nj, bjrs, zy FROM class ";
			ResultSet rs = stmt.executeQuery(sql);
			// 展开结果集数据库
			while (rs.next()) {
				// 通过字段检索
				int bjbh = rs.getInt("bjbh");
				String bjmc = rs.getString("bjmc");	
				int nj = rs.getInt("nj");	
				String zy = rs.getString("zy");	
				// 输出数据
                System.out.print("bjbh");
                System.out.print("bjmc ");
                System.out.print("nj ");
                System.out.print("zy ");
                System.out.print("\n");
				Class1 class2 = new Class1(bjbh, bjmc, zy, nj);
				classList.add(class2);
			}
			// 完成后关闭
			rs.close();
			stmt.close();
		} catch (SQLException se) {
			// 处理 JDBC 错误
			se.printStackTrace();
		} catch (Exception e) {
			// 处理 Class.forName 错误
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException se2) {
			}
		}
		System.out.println("查询成功!");
		return classList;
}
	public static int getBjbh(String bjmc) {
		Connection conn=ConnectionUtil.getConnection();
		String sql="select bjbh FROM class where bjbh like '%"+bjmc+"%'";
		Statement stat;
		try {
			stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sql);			
			if(rs.next()) {
				int bjbh = rs.getInt("bjbh");
				return bjbh;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return 201;
	}
	public static void addUsePrepareStatement(Class1 class2) {
		Connection conn = ConnectionUtil.getConnection();
		String sql = "insert into class values(?,?,?,?,?,?)";
		try {
			PreparedStatement psta = (PreparedStatement) conn.prepareStatement(sql);
			psta.setInt(1, class2.getBjbh());
			psta.setString(2, class2.getBjmc());
			psta.setString(3, class2.getZy());
			psta.setInt(4, class2.getNj());
			
			
			int result = psta.executeUpdate();
			if(result > 0) {
				System.out.println("插入成功");
			}else {
				System.out.println("插入失败");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		
     	}		
	}

	public static void del(String bjmc) {
		Connection conn = ConnectionUtil.getConnection();

		String sql = "delete from class where bjmc = '"+bjmc+"'";
		
		try {
			Statement stat = conn.createStatement();
			int result = stat.executeUpdate(sql);
			if(result > 0) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
		
}

